$(function(){
	/**项目指标树*/
	var _pi_tree = $('#pi_tree');
//	console.log(_pi_tree.length);
	if(_pi_tree) {
//		console.log('-------->>>exist tree');
		$('#pi_tree').tree({
		    url : 'projectIndex/projectIndexs',
		    onLoadSuccess:function(node,data){
		         var t = $(this);
		  		 if(data){
				     /*$(data).each(function(index,d){
						if(this.state == 'closed'){
						    t.tree('expandAll');
						}
				     });*/
				     var roots = t.tree('getRoots');
				     if (roots.length > 0) {
				    	 $(roots).each(function(index,root){
				    		 // console.log(root);
				    		 t.tree('expand', root.target);
			        	 });
				     }
				}
		    },
		    onClick : function (node) {
		    	//console.log(node);
		    	$('#dg').datagrid('reload', {
		    		name : $('#remark').val(),
		    		projectIndexId : node.id
		    	});
		    }
		});
	}/* else {
		console.log('-------->>>no tree');
	}*/
	
	/** 新增 */
	$('#add').click(function(){
		var _url = 'taskWorkflow/add.shtml';
		var node = $('#pi_tree').tree('getSelected');
		if (null != node) {
			var _projectIndexId = node.id;
			_url = _url + '?&projectIndexId=' + _projectIndexId;
		}
		showEasyUiWindow('contentFrame', _url, 'win', '新增任务', 500, 375, true);
	});
	
	/** 任务反馈 */
	$('#feedback').click(function(){
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			var task_status = row.status;
			var wf_status = row.wf_status;
			if (task_status == '0' || wf_status == '1') {
				$.messager.alert('warning', '当前任务未审批结束，请在审批结束后进行任务反馈操作', 'warning');
			} else if (task_status == '-1') {
				$.messager.alert('warning', '当前任务审批未通过，不可进行任务反馈操作', 'warning');
			} else if (task_status == '4') {
				$.messager.alert('warning', '当前任务已经作废，不可进行任务反馈', 'warning');
			} else {
				// 只有审批通过才可进行任务反馈操作
				//var url = 'task/detail.html?id=' + row.id + '&type=feedback';
				//showEasyUiWindow('contentFrame', url, 'win', '任务反馈', 350, 300, true);
				
				var url = 'task/feedbackTabs.html?id=' + row.id + '&type=feedback';
				showEasyUiWindow('contentFrame', url, 'win', '任务反馈', 350, 300, true);
			}
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/*** 编辑 */
	/*$('#edit').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			var url = 'task/edit.html?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '编辑任务', 500, 375);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});*/
	
	/**查看*/
	$('#detail').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			var url = 'task/detailTabs.html?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '查看任务', 350, 300, true);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/*** 删除 */
	/*$('#remove').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			$.messager.confirm('确认', '你确认要删除此条数据吗?', function(r) {
				if (r) {
					var id = row.id;				
					$.get('task/delete.html', {id : id}, function(rs) {
						if (rs.success == true) {
							reSearch();
							$.messager.alert('warning', rs.message, 'warning');
						} else {
							$.messager.alert('warning', rs.message, 'warning');
						}
					});
				}
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});*/
	
	/**迁移*/
	/*$('#move').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			var url = 'task/move.html?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '编辑任务', 350, 300, true);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});*/
	
	/**办理*/
	$('#todo').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			//var url = 'taskWorkflow/handle.html?taskId=' + row.dbid_ + '&instanceId=' + row.instance_id + '&id=' + row.id;
			var url = 'taskWorkflow/handleTabs.html?taskId=' + row.dbid_ + '&instanceId=' + row.instance_id + '&id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '任务审批', 500, 375, true);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	//管理
	$('#taskMgr').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			if(row.actor_id != $("#userId").val()){
				$.messager.alert('warning', '您不是该任务的责任人，无法进行计划管理操作！', 'warning');
			}else{
				var url = 'task/taskMgr.html?oper=planMgr&id=' + row.id;
				showEasyUiWindow('contentFrame', url, 'win', '编辑任务', 350, 300, true);
			}
			
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/** 作废 */
	$('#invalid').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			$.messager.confirm('确认', '你确认要作废此条数据吗?', function(r) {
				if (r) {
					var data = {};
					var _id = row.id;
					data.id = _id;
					if (row.instance_id) {
						var _instanceId = row.instance_id;
						data.instanceId = _instanceId;
					}
					$.get('taskWorkflow/invalid.shtml', data, function(rs) {
						if (rs.success == true) {
							reSearch();
							$.messager.alert('warning', '作废成功', 'warning');
						} else {
							$.messager.alert('warning', '作废失败，请联系管理员', 'warning');
						}
					});
				}
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
});

/**
 * 重载
 */
function reSearch() {
	$('#name').val('');
	$('#remark').val('');
	if ($('#progress_status').length > 0) {
		$('#progress_status').combobox('setValue', '');
	}
	if ($('#wf_status').length > 0) {
		$('#wf_status').combobox('setValue', '');
	}
	doSearch();
}


function doSearch() {
	var _progressStatus = '';
	if ($('#progress_status').length > 0) {
		_progressStatus = $('#progress_status').combobox('getValue');
	}
	var _status = '';
	if ($('#wf_status').length > 0) {
		_status = $('#wf_status').combobox('getValue');
	}
	$('#dg').datagrid('reload', {
		name : $('#name').val(),
		remark : $('#remark').val(),
		progressStatus : _progressStatus,
		status : _status
	});
}

